#
# @lc app=leetcode.cn id=160 lang=python3
#
# [160] 相交链表
#

# @lc code=start
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        if not headA or not headB:
            return None
        AHead, BHead = headA, headB
        while AHead != BHead:
            if not AHead:
                AHead = headB
            else:
                AHead = AHead.next
            if not BHead:
                BHead = headA
            else:
                BHead = BHead.next
        return AHead
        
# @lc code=end

